package com.eonmind.knowledge.service.impl;

import com.eonmind.common.contract.data.response.PageResponse;
import com.eonmind.common.contract.exception.BusinessException;
import com.eonmind.common.provider.entity.knowledge.Knowledge;
import com.eonmind.knowledge.converts.KnowledgeConvert;
import com.eonmind.knowledge.core.BailianKnowledgeService;
import com.eonmind.knowledge.dto.KnowledgeDto;
import com.eonmind.knowledge.mapper.KnowledgeMapper;
import com.eonmind.knowledge.service.KnowledgeService;
import com.eonmind.knowledge.service.KnowledgeUploadFileService;
import com.eonmind.user.contract.dto.UserInfoData;
import com.mongoplus.model.PageResult;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;


/**
 * @Author: AKang
 * @Description:
 * @CreateTime: 2025-05-09
 */
@Service
public class KnowledgeServiceImpl implements KnowledgeService {

    @Resource
    private KnowledgeMapper knowledgeMapper;

    @Resource
    private BailianKnowledgeService bailianKnowledgeService;

    @Resource
    private KnowledgeUploadFileService knowledgeUploadFileService;

    /**
     * @description: 创建知识库
     * @author: AKang
     * @date: 2025/5/11 15:47
     **/
    @Override
    public void saveKnowledge(KnowledgeDto.KnowledgeCreateRequest knowledgeCreateRequest, UserInfoData userInfoData) {
        Knowledge knowledge1 = knowledgeMapper.getKnowledgeByName(knowledgeCreateRequest.getName(), userInfoData.getId());
        if (ObjectUtils.isEmpty(knowledgeCreateRequest) || !ObjectUtils.isEmpty(knowledge1)) {
            throw new BusinessException("知识库已存在或参数错误!");
        }
        //封装为Knowledge实体
        Knowledge knowledge = KnowledgeConvert.toKnowledge(knowledgeCreateRequest);
        //插入知识库的创建者ID(userId)
        knowledge.setUserId(userInfoData.getId());
        //插入百炼知识库的indexId和IndexName
        KnowledgeDto.BailianKnowledgeResponse bailianKnowledge = bailianKnowledgeService.createBailianKnowledge(knowledgeCreateRequest, userInfoData);
        knowledge.setIndexName(bailianKnowledge.getIndexName());
        knowledge.setIndexId(bailianKnowledge.getIndexId());

        knowledgeMapper.saveKnowledge(knowledge);
    }

    /**
     * 更新知识库
     *
     * @param knowledgeUpdateRequest
     */
    @Override
    public void updateKnowledge(KnowledgeDto.KnowledgeUpdateRequest knowledgeUpdateRequest, UserInfoData userInfoData) {
        Knowledge knowledgeMapperById = knowledgeMapper.getById(knowledgeUpdateRequest.getKnowledgeId());
        if (ObjectUtils.isEmpty(knowledgeUpdateRequest) || ObjectUtils.isEmpty(knowledgeMapperById)) {
            throw new BusinessException("参数错误或知识库不存在!");
        }
        if (ObjectUtils.compare(knowledgeMapperById.getUserId(), userInfoData.getId()) != 0) {
            throw new BusinessException("操作的知识库非法!");
        }
        Knowledge knowledge = KnowledgeConvert.requestToKnowledge(knowledgeUpdateRequest);
        knowledge.setUserId(userInfoData.getId());
        knowledgeMapper.updateKnowledge(knowledge);
    }

    /**
     * 删除知识库
     *
     * @param knowledgeDeleteRequest
     */
    @Override
    public void deleteKnowledge(KnowledgeDto.KnowledgeDeleteRequest knowledgeDeleteRequest, UserInfoData userInfoData) {
        if (ObjectUtils.isEmpty(knowledgeDeleteRequest) || !knowledgeMapper.exist(knowledgeDeleteRequest.getKnowledgeId())) {
            throw new BusinessException("参数错误或知识库已不存在!");
        }
        if (getKnowledgeByKnowledgeIdAndUserId(knowledgeDeleteRequest.getKnowledgeId(), userInfoData.getId())) {
            throw new BusinessException("操作的知识库非法!");
        }
        //同步删除百炼平台的知识库
        String bailianKnowledgeIndexId = knowledgeMapper.getBailianKnowledgeIndexId(knowledgeDeleteRequest.getKnowledgeId());
        bailianKnowledgeService.deleteBailianKnowledge(bailianKnowledgeIndexId);

        //同步删除知识库关联的文件上传记录
        knowledgeUploadFileService.deleteKnowledgeUploadFile(knowledgeDeleteRequest.getKnowledgeId());

        knowledgeMapper.deleteKnowledge(knowledgeDeleteRequest, userInfoData);
    }

    /**
     * 获取知识库列表
     *
     * @param userInfoData
     * @param pageRequest
     * @return
     */
    @Override
    public PageResponse<KnowledgeDto.KnowledgeResponse> listKnowledge(UserInfoData userInfoData, KnowledgeDto.KnowledgePageRequest pageRequest) {
        PageResponse<KnowledgeDto.KnowledgeResponse> pageResponse = new PageResponse<>();
        if (ObjectUtils.isEmpty(pageRequest) || ObjectUtils.isEmpty(pageRequest.getPage()) || ObjectUtils.isEmpty(userInfoData)) {
            return pageResponse;
        }

        PageResult<KnowledgeDto.KnowledgeResponse> knowledgePageResult = knowledgeMapper.listKnowledge(userInfoData, pageRequest);

        pageResponse.setValue(pageRequest.getPage(), knowledgePageResult.getTotalSize(), knowledgePageResult.getContentData());
        return pageResponse;
    }


    /**
     * 根据indexId获取indexName
     *
     * @param indexId
     * @return
     */
    @Override
    public String getIndexNameByIndexId(String indexId) {
        if (ObjectUtils.isEmpty(indexId)) {
            throw new BusinessException("百炼知识库indexId参数错误!");
        }
        return knowledgeMapper.getIndexNameByIndexId(indexId);
    }

    /**
     * 校验操作知识库是否为该用户所属
     *
     * @param knowledgeId
     * @param userId
     * @return
     */
    @Override
    public boolean getKnowledgeByKnowledgeIdAndUserId(Long knowledgeId, Long userId) {
        if (ObjectUtils.isEmpty(knowledgeId) || ObjectUtils.isEmpty(userId)) {
            throw new BusinessException("参数错误!");
        }
        Knowledge knowledge = knowledgeMapper.getKnowledgeByKnowledgeIdAndUserId(knowledgeId, userId);
        return !ObjectUtils.isEmpty(knowledge);
    }


}
